<!doctype html>
<html lang="en">
<meta http-equiv="Content-Type"content="text/html;charset=utf-8">


<head>
    <title>OAUTH2 授权登哭 / OpenId Connect 协议</title>
    <style>
        body {
            margin: 50px 50px;
        }
        a {
            display: block;
            line-height: 40px;
        }
    </style>
</head>
<body>
    <h1>自定义模板 OAUTH2 授权登哭 / OpenId Connect 协议</h1>
    <div class="container">
        <div id="login" style="display:block">
            <a href="/oauth2/authorization/merchant">   自定义Oauth2-client-授权登录  </a>
            <a href="/oauth2/authorization/gitee">      GITEE-授权登录  </a>
        </div>
        <div id="welcome" style="display:none">
            欢迎 <span id="name"></span> | <button onClick="logout()">退出登录</button>
        </div>
    </div>
    <script>
        // fetch('/user')
        //     .then(response => {
        //         if (response.ok) {
        //             return response.json();
        //         } else {
        //             throw new Error('Something went wrong');
        //         }
        //     })
        //     .then(data => {
        //         document.getElementById('name').innerText = data.name;
        //         document.getElementById('login').style.display = 'none';
        //         document.getElementById('welcome').style.display = 'block';
        //     })
        //     .catch((error) => {
        //         console.error('Error: ', error);
        //     });

        function logout() {
            fetch('/logout', {
                    method: 'POST',
                    headers: {
                        'X-XSRF-TOKEN': getCookie('XSRF-TOKEN')
                    }
                })
                .then((response) => {
                    if (response.ok) {
                        document.getElementById('login').style.display = 'block';
                        document.getElementById('welcome').style.display = 'none';
                    } else {
                        throw new Error('Something went wrong');
                    }
                })
                .catch((error) => {
                    console.error('Error: ', error);
                });
        }

        function getCookie(name) {
            var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
            return v ? v[2] : null;
        }
    </script>
</body>
</html>